<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

/**
 * Class User
 *
 * @property int $id
 * @property string|null $nick_name
 * @property string|null $avatar
 * @property string|null $openid
 * @property string|null $mobile
 * @property int $sex
 * @property Carbon|null $birthday
 * @property string|null $province
 * @property string|null $city
 * @property string|null $area
 * @property float|null $balance
 * @property int $pid
 * @property string|null $source
 * @property int $level
 * @property Carbon|null $partner_end
 * @property float $commission_amount
 * @property float $order_amount
 * @property int $order_count
 * @property float $withdraw_amount
 * @property Carbon|null $create_at
 * @property Carbon|null $update_at
 * @property Carbon|null $delete_at
 *
 * @package App\Models
 */
class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, Page;

    protected $table = 'zh_users';

    const UPDATED_AT = 'update_at';

    const CREATED_AT = 'create_at';

    const DELETED_AT = 'delete_at';

    const L_DEFAULT = 1; //身份 普通会员

    const L_DISTRIBUTOR = 2; //身份 经销商

    const L_PARTNER = 3; //身份 合伙人

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    const LEVEL_GOODS_COMPARE = [
        self::L_DEFAULT => 'market_price',
        self::L_DISTRIBUTOR => 'distributor_price',
        self::L_PARTNER => 'partner_price',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'nick_name',
        'avatar',
        'openid',
        'mobile',
        'sex',
        'birthday',
        'province',
        'city',
        'area',
        'balance',
        'pid',
        'source',
        'level',
        'partner_end',
        'commission_amount',
        'order_amount',
        'order_count',
        'create_at',
        'update_at',
        'delete_at',
        'withdraw_amount',
    ];

    protected $appends = [
        'sex_string', 'pid_string', 'level_string', 'source_string'
    ];

    public function getSexStringAttribute()
    {
        switch ($this->sex){
            case 1:
                return '男';
            case 2:
                return '女';
            default:
                return '未知';
        }
    }


    /**
     * @return string
     */
    public function getSourceStringAttribute()
    {
        switch ($this->source){
            case 'share_free':
                return '邀请助力';
            default:
                return '常规注册';

        }
    }

    /**
     * @return User|mixed
     */
    public function parents()
    {
       return self::find((int)$this->pid);
    }

    /**
     * @return string|null
     */
    public function getPidStringAttribute()
    {
        return $this->parents() ? $this->parents()->nick_name : '总店';
    }

    public function getLevelStringAttribute()
    {
        switch ($this->level){
            case self::L_DEFAULT:
                return '普通会员';
            case self::L_DISTRIBUTOR:
                return '经销商';
            case self::L_PARTNER:
                return '合伙人';
        }
    }
}
